//
//  XMModulersViewController.m
//  XMKit
//
//  Created by chenming on 2018/5/26.
//  Copyright © 2018年 ChenXiaoMing. All rights reserved.
//

#import "XMModulersViewController.h"

// Model
#import "XMModulersModel.h"

// View
#import "XMPlayVideoView.h"

@interface XMModulersViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) XMPlayVideoView *playVideoView;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@end

static NSString *const cellIdentifier = @"cellIdentifier";

@implementation XMModulersViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 启动视频
    _playVideoView = [[XMPlayVideoView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    __weak typeof(self) weakSelf = self;
    _playVideoView.playFinished = ^{
        [weakSelf.playVideoView removeFromSuperview];
        weakSelf.playVideoView = nil;
    };
    [_playVideoView show];

    [self.view addSubview:self.tableView];

    NSData *data = [NSData dataNamed:[NSString stringWithFormat:@"XMKitcell.json"]];
    NSDictionary *dict = [XMToolManager dictionaryWithJSON:data];
    XMModulersModel *model = [[XMModulersModel alloc] initWithXMDataModelDict:dict];
    [self.dataSource addObject:model];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationItem.title = @"模块";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    XMModulersModel *model = [self.dataSource firstObject];
    NSString *className = model.cellClass[indexPath.row];
    Class class = NSClassFromString(className);
    if (class) {
        UIViewController *ctrl = class.new;
        ctrl.title = model.cellData[indexPath.row];
        if (![className isEqualToString:@"XMPresentAlterViewController"]) {
            [self.navigationController pushViewController:ctrl animated:YES];
        } else {
            [self modalpresentedViewController:ctrl controllerHeight:kSCREENHEIGHT - 150];
            
        }
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    XMModulersModel *model = [self.dataSource firstObject];
    return model.cellData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    XMModulersModel *model = [self.dataSource firstObject];
    cell.textLabel.text = [model.cellData objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.textLabel.textColor = [UIColor colorWithHexString:@"333333" alpha:1];
    return cell;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _tableView.backgroundColor = [UIColor whiteColor];
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
